﻿using System;
using System.Collections.Generic;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using KPIS.GERP.GHRMS.BLL;
using KPIS.GERP.GHRMS.IBLL;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.DBM; 

namespace KPIS.GERP.GHRMS.APP.App_UI.UserControls
{
    public partial class DC_InvestDiscipline : System.Web.UI.UserControl
    {
        private SsMng ssMng = new SsMng();
        private object _dataItem = null;

        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public object DataItem
        {
            get
            {
                return this._dataItem;
            }
            set
            {
                this._dataItem = value;
            }
        }
        protected bool IsUserControlPostBack
        {
            get
            {
                return this.ViewState["IsUserControlPostBack"] != null;
            }
        }

  
        protected override void OnLoad(System.EventArgs e)
        {
            base.OnLoad(e);

            if (!IsUserControlPostBack)
            {
                this.ViewState.Add("IsUserControlPostBack", true);
            }

            if (DataItem != null)
            {
                BindCombo();

                if (!(DataItem is Telerik.Web.UI.GridInsertionObject))
                {
                    lblInvSeq.Text   = DataBinder.Eval(DataItem, "inv_seq").ToString();

                    txtInvDate.SetValue(DataBinder.Eval(DataItem, "inv_date", "{0:dd/MM/yyyy}").ToString());               
                    txtInvRefNo.Text = DataBinder.Eval(DataItem, "inv_ref_no").ToString();
                    txtInvName.Text    = DataBinder.Eval(DataItem, "inv_name").ToString();
                    comboParent.SelectedValue = DataBinder.Eval(DataItem, "inv_ref_seq").ToString();

                    RadioButtonList1.SelectedValue = DataBinder.Eval(DataItem, "appeals").ToString();

                    if (RadioButtonList1.SelectedValue == "1")
                    {
                        visibleAppeal("1");
                        txtAppealResult.Text = DataBinder.Eval(DataItem, "app_result").ToString();                      
                        txtAppDate.SetValue(DataBinder.Eval(DataItem, "app_date", "{0:dd/MM/yyyy}").ToString());
                    }
                    else
                    {
                        visibleAppeal("0");
                    }
                    txtAppealResult.Enabled = RadioButtonList1.SelectedValue == "1";
                
                }

            }
      
        }

        private void BindCombo()
        {
            try
            {
                InvestDesciplineBLL obj = new InvestDesciplineBLL();

                DataTable dt;
                dt = obj.SelectComboParent() ;

                comboParent.DataValueField = "inv_seq";
                comboParent.DataTextField = "inv_name";
                comboParent.DataSource = dt;
                comboParent.DataBind();  
 
            }
            catch (Exception ex)
            {

            }

        }

        protected void btnAddnew_Click(object sender, EventArgs e)
        {
            string invDate, invRefNO,app_date,app_result,invName;
            int inv_seq,appeals;

            invDate = txtInvDate.GetStringValue() ;
            invDate = Utils.Date .ToEngFormatDate (invDate);
            invRefNO = txtInvRefNo.Text;

            invName = txtInvName.Text;  
            inv_seq = int.Parse(comboParent.SelectedValue);

            
            app_date = txtAppDate.GetStringValue();
            app_date = Utils.Date.ToEngFormatDate(app_date);

            app_result = txtAppealResult.Text;   

            appeals = int.Parse(RadioButtonList1.SelectedValue);


            InvestDesciplineBLL obj = new InvestDesciplineBLL();

            if (appeals == 1)
            {
                obj.insert(inv_seq, invName, invRefNO, txtInvDate.GetStringValue().ToString(), appeals, app_date, app_result, ssMng.EmpSeqI);
            }
            else
            {
                obj.insertNoAppeal(inv_seq, invName, invRefNO, txtInvDate.GetStringValue().ToString(), appeals, ssMng.EmpSeqI);
            }
 
        }


        protected void btnUpdate_Click(object sender, EventArgs e)
        {



            string invDate, invRefNO,invName, app_date, app_result;
            int inv_seq, appeals,dataKey;
            

            dataKey = int.Parse(lblInvSeq.Text);

            invDate = txtInvDate.GetStringValue();
            invDate = Utils.Date.ToEngFormatDate(invDate);

            invRefNO = txtInvRefNo.Text;
            invName = txtInvName.Text;  

            inv_seq = int.Parse(comboParent.SelectedValue);

            appeals = int.Parse(RadioButtonList1.SelectedValue);

            InvestDesciplineBLL obj = new InvestDesciplineBLL();

            if (RadioButtonList1.SelectedValue == "1")
            {
                app_date = txtAppDate.GetStringValue();
                app_date = Utils.Date.ToEngFormatDate(app_date);
                app_result = txtAppealResult.Text;

                obj.Update(dataKey, invName, inv_seq, invRefNO, txtInvDate.GetStringValue().ToString(), appeals, app_date, app_result, ssMng.EmpSeqI);
            }
            else
            {

                obj.UpdateNoAppeal(dataKey, invName, inv_seq, invRefNO, txtInvDate.GetStringValue().ToString(), appeals, ssMng.EmpSeqI);
            }

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        private void visibleAppeal(string value)
        {
            if (value == "1")
            {
                //  txtAppealDate.Enabled = true;

                lblAppResult.Visible = true;
                txtAppealResult.Enabled = true;
                txtAppealResult.Visible = true;

                lblAppDate.Visible = true;
                txtAppDate.Visible = true;
                
                txtAppealResult.Enabled = true;
                txtAppealResult.Text = DataBinder.Eval(DataItem, "app_result").ToString();

                
                string mydate = DataBinder.Eval(DataItem, "app_date").ToString();  // --------  4/15/2009 12:00:00 AM
                string[] str = mydate.Split('/');
                str[2] = (int.Parse(str[2].Substring(0, 4)) + 543).ToString();
                mydate = str[1] + "/" + str[0] + "/" + str[2];
                txtAppDate.SetValue(mydate);

            }
            else
            {
                //txtAppealDate.Enabled = false ;

                lblAppResult.Visible = false;
                txtAppealResult.Enabled = false;
                txtAppealResult.Visible = false;

                lblAppDate.Visible = false;
                txtAppDate.Visible = false;

            }


        }

        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            bool bVisible = RadioButtonList1.SelectedValue == "1";

            lblAppResult.Visible = bVisible;
            txtAppealResult.Enabled = bVisible;
            txtAppealResult.Visible = bVisible;

            lblAppDate.Visible = bVisible;
            txtAppDate.Visible = bVisible;
        }
    }
}